package ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import org.apache.log4j.Logger;
import client.ClientEntry;
import controller.LoginController;

public class LoginWin extends JFrame {
	
	private static final Logger LOGGER = Logger.getLogger(LoginWin.class);
	
	private static final long serialVersionUID = 1L;
	private JTextField nameTf;
	private JPasswordField pwdPf;
	private JButton loginBtn;
	private JButton registBtn;
	private JLabel nameLa;
	private JLabel pwdLa;
	private JPanel errorMsgPanel;
	
	public LoginWin(){
		this.setSize(400, 300);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("IM Login");
		
		init();
		addEvent();
		this.setVisible(true);
	}

	public JTextField getNameTf() {
		return nameTf;
	}

	public void setNameTf(JTextField nameTf) {
		this.nameTf = nameTf;
	}

	public JPasswordField getPwdPf() {
		return pwdPf;
	}

	public void setPwdPf(JPasswordField pwdPf) {
		this.pwdPf = pwdPf;
	}

	private void init(){
		this.setLayout(null);
		nameTf = new JTextField();
		pwdPf = new JPasswordField();
		loginBtn = new JButton("Login");
		registBtn = new JButton("Register");
		nameLa = new JLabel("User");
		pwdLa = new JLabel("Password");
		errorMsgPanel = new JPanel();
		
		nameTf.setBounds(100, 50, 260, 30);
		pwdPf.setBounds(100, 110, 260, 30);
		loginBtn.setBounds(90, 195, 90, 30);
		registBtn.setBounds(210, 195, 90, 30);
		nameLa.setBounds(30, 50, 60, 30);
		pwdLa.setBounds(30, 110, 60, 30);
		errorMsgPanel.setBounds(30, 120, 260, 30);
		
		this.add(nameTf);
		this.add(pwdPf);
		this.add(loginBtn);
		this.add(registBtn);
		this.add(nameLa);
		this.add(pwdLa);
		this.add(errorMsgPanel);
	}
	private void addEvent(){
		
		nameTf.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					if((nameTf.getText()!=null) && pwdPf.getPassword()!=null){
						doLogin();
					}
				}
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
			}
		});
		
		pwdPf.addKeyListener(new KeyListener() {
			
			@Override
			public void keyReleased(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					if((nameTf.getText()!=null) && pwdPf.getPassword()!=null){
						doLogin();
					}
				}
			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		loginBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				doLogin();
			}
		});
		
		registBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				LoginWin.this.dispose();
				RegistWin.current.setVisible(true);
			}
			
		});
	}
	
//	public static void main(String[] args) throws Exception {
//		new LoginWin();
//	}
	
	private boolean checkNotEmpty() {
		if (nameTf.getText() == null || nameTf.getText().isEmpty()) {
			JOptionPane.showMessageDialog(LoginWin.this, "Username empty!", "Error", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		
		if (pwdPf.getPassword() == null || pwdPf.getPassword().length == 0) {
			JOptionPane.showMessageDialog(LoginWin.this, "Password empty!", "Error", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		
		return true;
	}
	
	private void doLogin() {
		if (!checkNotEmpty()) {
			return;
		}
		
		String name = nameTf.getText();
		String pwd = new String(pwdPf.getPassword());
		
		try {
			new LoginController().control(name, pwd);
		} catch (IOException e1) {
			LOGGER.error(e1.toString());
			JOptionPane.showMessageDialog(LoginWin.this, "The server is crashed!", "Error", JOptionPane.ERROR_MESSAGE);
		}
		System.out.println("login...."+name+".."+pwd);
	}
}
